/**
 * 
 */
package com.wubo.sec.filter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.wubo.sec.SecurityManager;
import com.wubo.sec.model.Group;
import com.wubo.sec.service.IGroupService;
import com.wubo.sec.util.ArrayUtils;

/**
 * @author WuBo
 * @CreateDate 2011-2-21
 * @version 1.0.1
 */
public class ActionLogBeforeFilter implements IDataBeforeFilter {

	private IGroupService groupService;
	

	public ActionLogBeforeFilter(IGroupService groupService){
		this.groupService = groupService;
	}
	/* (non-Javadoc)
	 * @see com.asc.sf.filter.IDataBeforeFilter#sift()
	 */
	public String sift() {
		Group group = SecurityManager.getCurrentUser().getGroup();
		
		String code = SecurityManager.getCurrentUser().getUsername();
		
		int priority = group.getPriority();
		String mark = group.getMark();
		
		//codes.add(group.getCode());
		
		
		List<Group> groups = groupService.findAccessibleGroups(priority, mark, group);
		
		if(groups.size() > 0){
			Set<String> codes = new HashSet<String>();
			for(int i=0; i<groups.size(); i++){
				codes.add(groups.get(i).getCode());
			}
			return " and ( l.user.group.code in (" + ArrayUtils.split(codes.toArray(), ",", "'") + ") or l.user.owner = '"
				+ code + "' or l.user.username = '"
				+ code + "')";
		} else {
			return " and (l.user.owner = '"+code+"' or l.user.username = '"+code+"')";
		}
	}

}
